package com.hugo.eduservice.controller;


import com.hugo.commonutiles.R;
import com.hugo.eduservice.entity.EduChapter;
import com.hugo.eduservice.entity.chapter.ChapterVo;
import com.hugo.eduservice.entity.subject.OneSubject;
import com.hugo.eduservice.service.EduChapterService;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hugo
 * @since 2020-09-08
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;

    //获取章节和小结
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = eduChapterService.getTreeChapter(courseId);
        return R.ok().data("allChapterVideo",list);
    }
    //添加课程
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();

    }
    //根据章节id查询
    @GetMapping("getChapterInfo/{courseId}")
    public R getChapterInfo(@PathVariable String courseId){
        EduChapter eduChapter = eduChapterService.getById(courseId);
        return R.ok().data("eduChapter",eduChapter);
    }
    //修改章节
    @PostMapping("UpdateChapter")
    public R UpdateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @DeleteMapping("{courseId}")
    public R DeleteChapter(@PathVariable String courseId){
        boolean b = eduChapterService.deleteChapter(courseId);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }
    }


}

